﻿using SaaS.Core;
using SaaS.Model.Enums;
using System;
using XCore.Extensions;
using XCore.Helper;
using XCore.Logger;

namespace Web.Client.Utils
{
    public class ClientLogger : LoggerBase
    {
        /// <summary>
        /// 用户登录信息
        /// </summary>
        private ClientSession Session { get; }
        public ClientLogger(ClientSession session) : base(CoreSetting.RedisConnectionString, CoreSetting.CLIENTLOG_KEY, CoreSetting.LOG_INDEX)
        {
            Session = session;
        }
        public bool Log(int userId, string message, ClientLoggerSource source)
        {
            LoggerInfo ei = new LoggerInfo
            {
                Message = message
            };
            ei.Properties["UserId"] = userId;
            ei.Properties["Source"] = (byte)source;
            ei.Properties["Duration"] = StopwatchHelper.Milliseconds;
            ei.Properties["IP"] = HttpContext.GetIp();
            ei.Properties["SystemType"] = HttpContext.GetOperatingType();
            ei.Properties["BrowserInfo"] = HttpContext.UserAgent();
            ei.Properties["CustomData"] = HttpContext.GetRequestInfo();
            ei.Properties["CreationTime"] = DateTime.Now;
            this.RedisLog(ei);
            return true;
        }
        public bool Log(string message, ClientLoggerSource source)
        {
            return this.Log(Session.UserId, message, source);
        }

        public bool Log(string message)
        {
            return this.Log(message, ClientLoggerSource.INFO);
        }
    }
}
